package ru.dc.gui.swing.util;

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang.StringUtils;
import ru.dc.Application;
import ru.dc.object.FileInfo;
import ru.dc.object.ServerInfo;

import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import java.awt.*;
import java.util.HashMap;
import java.util.Map;

/**
 * <pre>
 * User: Penkov Vladimir
 * Date: 02.11.2010
 * Time: 15:22:49
 * </pre>
 */
public class FilenameRenderer extends DefaultTableCellRenderer {

    public static final Map<String, String> iconsByExt = new HashMap<String, String>();
    static {
        iconsByExt.put("mkv", "icon_video.gif");
        iconsByExt.put("avi", "icon_video.gif");
        iconsByExt.put("mpg", "icon_video.gif");
        iconsByExt.put("mpeg", "icon_video.gif");
        iconsByExt.put("divx", "icon_video.gif");
        iconsByExt.put("mov", "icon_video.gif");
        iconsByExt.put("wmv", "icon_video.gif");
        iconsByExt.put("asf", "icon_video.gif");
        iconsByExt.put("vob", "icon_video.gif");

        iconsByExt.put("mp3", "audio_icon.gif");
        iconsByExt.put("wav", "audio_icon.gif");
        iconsByExt.put("wma", "audio_icon.gif");
        iconsByExt.put("ogg", "audio_icon.gif");
        iconsByExt.put("flac", "audio_icon.gif");

        iconsByExt.put("iso", "iso_icon.png");
        iconsByExt.put("mdf", "iso_icon.png");
        iconsByExt.put("nrg", "iso_icon.png");

        iconsByExt.put("rar", "archive-icon.gif");
        iconsByExt.put("zip", "archive-icon.gif");
        iconsByExt.put("bz2", "archive-icon.gif");
        iconsByExt.put("gzip", "archive-icon.gif");
        iconsByExt.put("7z", "archive-icon.gif");

        iconsByExt.put("exe", "exe_icon.gif");

        iconsByExt.put("jpg", "icon_image.gif");
        iconsByExt.put("jpeg", "icon_image.gif");
        iconsByExt.put("gif", "icon_image.gif");
        iconsByExt.put("ico", "icon_image.gif");
        iconsByExt.put("bmp", "icon_image.gif");
        iconsByExt.put("tiff", "icon_image.gif");
        iconsByExt.put("psd", "icon_image.gif");
    }

    public static final String DEFAULT_ICON = "file_icon.gif";
    public static final String DIRECTORY_ICON = "icon_dir.gif";

    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        JLabel label = (JLabel) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
        if (value!=null) {
            String filename = value.toString();
            String ext = FilenameUtils.getExtension(filename).toLowerCase();
            String iconName;
            if (StringUtils.isEmpty(ext)) {
                iconName = DIRECTORY_ICON;
            }
            else {
                iconName = iconsByExt.get(ext);
                if (iconName==null) {
                    iconName = DEFAULT_ICON;
                }
            }

            Icon icon = IconLoader.getInstance().getIcon(iconName);
            label.setIcon(icon);
        }
        return label;
    }
}
